﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Bll.CommonBll;
using CSP.Bll.IObjectBlls;
using CSP.Dao.IObjectDaos.CSP;
using CSP.Common.Factories;
using CSP.Dao;
using CSP.Dao.ObjectDaos.CSP;
using System.Collections.ObjectModel;
using CSP.Model.CSP;

namespace CSP.Bll.ObjectBlls
{
    public class DomainTypeBll : CSPCommonBll, IDomainTypeBll
    {
        /// <summary>
        /// 缓存键值
        /// </summary>
        private const string CACHEKEYALL = "DomainTypeAll";

        private IDomainTypeDao _domainTypeDao;

        public DomainTypeBll()
        {
            _cacheProvider = CachedProviderFactory.GetLocalCachedProvider();
            _domainTypeDao = CSPDaoFactory.GetInstance().GetDomainTypeDao();
        }

        public ICollection<DomainType> FindAll()
        {
            //检测缓存是否存在
            ICollection<DomainType> domainTypes = _cacheProvider.GetData<DomainType[]>(CACHEKEYALL);
            if (domainTypes == null)
            {
                //从数据库读取数据
                domainTypes = _domainTypeDao.FindAll();
                _cacheProvider.AddData(CACHEKEYALL, domainTypes, DateTime.Now.AddHours(10));
            }
            return domainTypes;
        }

        public ICollection<DomainType> FindAllByAvailable()
        {
            ICollection<DomainType> domainTypes = this.FindAll();
            ICollection<DomainType> result = null;
            if (domainTypes != null)
            {
                //获取有效的数据
                var temDomainTypes = domainTypes.Where(s => s.Status == false);
                if (temDomainTypes != null)
                {
                    result = temDomainTypes.ToArray();
                }
            }
            return result;
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DomainType GetDataById(int id)
        {
            DomainType domainType = GetById(id);
            if (domainType == null)
            {
                FindAll();
                domainType = GetById(id);
            }

            return domainType;
        }

        /// <summary>
        /// 根据Ids 获取数据
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public ICollection<DomainType> GeDatatByIds(int[] Ids)
        {
            ICollection<DomainType> domainType = GetByIds(Ids);
            if (domainType == null || domainType.Count == 0)
            {
                FindAll();
                domainType = GetByIds(Ids);
            }

            return domainType;
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="obj"></param>
        public void Add(DomainType obj)
        {
            if (obj == null)
            {
                throw new Exception("DomainType Can't be null");
            }
            _domainTypeDao.Add(obj);

            //清除缓存
            this.RefreshCache();

        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="obj"></param>
        public void Update(DomainType obj)
        {
            if (obj == null)
            {
                throw new Exception("DomainType Can't be null");
            }
            _domainTypeDao.Update(obj);

            //清除缓存0
            this.RefreshCache();

        }

        public void DeleteById(int id)
        {
            _domainTypeDao.DeleteById(id);

            //清除缓存0
            this.RefreshCache();
        }

        /// <summary>
        /// 刷新Cache
        /// </summary>
        public void RefreshCache()
        {
            //删除缓存Key
            _cacheProvider.Remove(CACHEKEYALL);
        }

        /// <summary>
        /// 通过Id 获取领域类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private DomainType GetById(int id)
        {
            DomainType domainType = null;

            //检测缓存是否存在
            ICollection<DomainType> domainTypes = GetByIds(new int[] { id });
            if (domainTypes != null && domainTypes.Count != 0)
            {
                domainType = domainTypes.FirstOrDefault();
            }
            return domainType;
        }

        private ICollection<DomainType> GetByIds(int[] ids)
        {
            ICollection<DomainType> result = new Collection<DomainType>();

            //检测缓存是否存在
            ICollection<DomainType> domainType = _cacheProvider.GetData<ICollection<DomainType>>(CACHEKEYALL);
            foreach (var item in domainType)
            {
                if (ids.Contains(item.Id))
                {
                    result.Add(item);
                }
            }
            return result;
        }

    }
}
